package org.arch_learn.netty_rpc.consumer.config;

import org.arch_learn.netty_rpc.consumer.handler.NettyRpcClientHandler;
import org.arch_learn.netty_rpc.consumer.netty_client.NettyRpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
@ConfigurationProperties(prefix = "netty.server")
public class NettyConfig {
    private List<Map<String, Object>> list;

    public void setList(List<Map<String, Object>> list) {
        this.list = list;
    }

    @Bean
    public List<NettyRpcClient> nettyRpcClients() {
        return list.stream()
                .map(map -> {
                    String ip = (String) map.get("ip");
                    Integer port = (Integer) map.get("port");
                    NettyRpcClient nettyRpcClient = new NettyRpcClient(ip, port);
                    //多客户端实例情况下不能公用一个handler实例，否则容易出bug
                    nettyRpcClient.initClient(new NettyRpcClientHandler());
                    return nettyRpcClient;
                }).collect(Collectors.toList());
    }
}
